﻿//
// eCRAM, a monthly activity report web application
// Copyright (C) 2010  Aptea
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System.Linq;

using eCRAM.Model;
using eCRAM.Bll;

namespace eCRAM.WebSite.Services
{
    /// <summary>
    /// Authenticates a user using the CRAM user table from our database.
    /// </summary>
    public class AuthenticationService : IAuthenticationService
    {
        /// <summary>
        /// User table/repository.
        /// </summary>
        private readonly IUserManager _userManager;

        /// <summary>
        /// Creates the authentification service given the CRAM user table/repository.
        /// </summary>
        /// <param name="userRepo">User repository</param>
        public AuthenticationService(IUserManager userRepo)
        {
            _userManager = userRepo;
        }

        /// <summary>
        /// Logs on a user if its username/password matches a user from the database.
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns>The matching user or null if authentification failed</returns>
        public User LogOn(string userName, string password)
        {
            try
            {
                //FIXME I guess the password is not encrypted...
                return _userManager.GetUser( userName,password);
            }
            catch
            {
                return null;
            }
        }
    }
}
